<template>
  <div>
    <el-dialog :visible.sync="$parent.open" :title="title">
      <el-form ref="elForm" :model="formData" :rules="rules" size="medium" label-width="120px">
        <div class="margin_div">
          <el-form-item label="任务责任部门" prop="deptId">
            <el-select v-model="formData.deptId" @change="handleChange(formData.deptId, 1)" placeholder="请选择任务责任部门"
              :style="{ width: '100%' }">
              <el-option v-for="(item, index) in deptIdOptions" :key="index" :label="item.deptName" :value="item.deptId"
                :disabled="item.disabled"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="任务责任人" prop="headId">
            <el-select v-model="formData.headId" placeholder="请选择任务责任人" :style="{ width: '100%' }">
              <el-option v-for="(item, index) in headIdOptions" :key="index" :label="item.userName" :value="item.userId"
                :disabled="item.disabled"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="盘点范围" prop="stktakingDeptId">
            <el-select v-model="formData.stktakingDeptId" placeholder="请选择盘点范围" :style="{ width: '100%' }">
              <el-option v-for="(item, index) in deptIdOptions" :key="index" :label="item.deptName" :value="item.deptId"
                :disabled="item.disabled"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="盘点方式" prop="type">
            <el-select v-model="formData.type" placeholder="请选择盘点方式" :style="{ width: '100%' }">
              <el-option v-for="(item, index) in typeOptions" :key="index" :label="item.label" :value="item.value"
                :disabled="item.disabled"></el-option>
            </el-select>
          </el-form-item>
          <template v-if="formData.type === '1'">
            <el-form-item label="抽盘比例" prop="proportion">
              <el-select v-model="formData.proportion" placeholder="请选择抽盘比例" :style="{ width: '100%' }">
                <el-option v-for="(item, index) in proportionOptions" :key="index" :label="item.label"
                  :value="item.value" :disabled="item.disabled"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="盘点对象生成" prop="generationType">
              <el-select v-model="formData.generationType" placeholder="请选择盘点对象生成" :style="{ width: '100%' }">
                <el-option v-for="(item, index) in generationTypeOptions" :key="index" :label="item.label"
                  :value="item.value" :disabled="item.disabled"></el-option>
              </el-select>
            </el-form-item>
          </template>
        </div>
      </el-form>
      <div slot="footer" align="center">
        <el-button @click="() => $parent.open = false">取消</el-button>
        <el-button type="primary" @click="handleConfirm">提交</el-button>
      </div>
    </el-dialog>
    <template v-if="open">
      <ChooseObject :title="title" :planId="planId" :deptId="formData.stktakingDeptId" :dataObj="formData">
      </ChooseObject>
    </template>
  </div>
</template>
<script>
import { getsystemlist, getuserlist } from "@/api/equipment/equipment.js";
import { postCreatedTask, putUpdateTask } from "@/api/Inventory/Inventory.js";
export default {
  components: {
    ChooseObject: () => import('./chooseObject.vue')
  },
  props: ['planId', 'title', 'rowObj'],
  data () {
    return {
      formData: {
        deptId: undefined,
        headId: undefined,
        stktakingDeptId: undefined,
        type: undefined,
        proportion: undefined,
        generationType: undefined,
        stktakingTaskEquipments: []
      },
      open: false,
      rules: {
        deptId: [{
          required: true,
          message: '请选择任务责任部门',
          trigger: 'change'
        }],
        headId: [{
          required: true,
          message: '请选择任务责任人',
          trigger: 'change'
        }],
        stktakingDeptId: [{
          required: true,
          message: '请选择盘点范围',
          trigger: 'change'
        }],
        type: [{
          required: true,
          message: '请选择盘点方式',
          trigger: 'change'
        }],
        proportion: [{
          required: true,
          message: '请选择抽盘比例',
          trigger: 'change'
        }],
        generationType: [{
          required: true,
          message: '请选择盘点对象生成',
          trigger: 'change'
        }],
      },
      deptIdOptions: [],
      headIdOptions: [],
      typeOptions: [{
        "label": "全面盘点",
        "value": '0'
      }, {
        "label": "比例抽盘",
        "value": '1'
      }],
      proportionOptions: [{
        "label": "10%",
        "value": 0.1
      }, {
        "label": "50%",
        "value": 0.5
      }, {
        "label": "80%",
        "value": 0.8
      }, {
        "label": "95%",
        "value": 0.95
      }],
      generationTypeOptions: [{
        "label": "随机生成",
        "value": '0'
      }, {
        "label": "手动生成",
        "value": '1'
      }],
    }
  },
  computed: {},
  watch: {},
  created () {
    this.getList();
  },
  mounted () { },
  methods: {
    async getList () {
      let params = await getsystemlist()
      this.deptIdOptions = params.data
      if (this.title === '修改任务') {
        this.formData = JSON.parse(JSON.stringify(this.rowObj))
        this.handleChange(this.rowObj.deptId)
      }
    },
    async handleChange (val, int) {
      if (int === 1) {
        this.formData.headId = undefined
      }
      let params = await getuserlist({ deptId: val })//负责人
      this.headIdOptions = params.data
    },
    handleConfirm () {
      this.$refs['elForm'].validate(valid => {
        if (!valid) return
        if (this.formData && this.formData.generationType === '1' && this.formData.type === '1') {//手动生成
          this.open = true
          return false
        }
        if (this.title === '修改任务') {
          let params = {
            ...this.formData,
            planId: this.planId
          }
          putUpdateTask(params).then(() => {
            this.$parent.open = false
            this.$parent.getList()
            this.$message.success('操作成功')
          })
        } else {
          let params = {
            ...this.formData,
            planId: this.planId
          }
          postCreatedTask(params).then(() => {
            this.$parent.open = false
            this.$message.success('操作成功')
            this.$parent.getList()
          })
        }
      })
    },
  }
}

</script>
<style lang="scss" scoped>
.margin_div {
  margin: 0 auto;
  width: 50%;
}
</style>